<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<html>
<head>
    <title>商品列表</title>
    <meta name="decorator" content="default"/>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#inputForm").validate({
                submitHandler: function(form){
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                errorContainer: "#messageBox",
                errorPlacement: function(error, element) {
                    $("#messageBox").text("输入有误，请先更正。");
                    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });
        });
    </script>
    <script type="text/javascript">
        function page(n, s) {
            $("#pageNo").val(n);
            $("#pageSize").val(s);
            $("#searchForm").attr("action", "${ctx}/sys/tbShopController/showAgentProductList");
            $("#searchForm").submit();
            return false;
        }
        //比较输入框的值
        function num1(a){
            $("#btn").attr("disabled", false);
            var shopnum=0;
            shopnum=$("#"+a).val();
            var agentnum=0;
            $.ajax({
                type:'post',
                url:'${ctx}/sys/tbShopController/checkProductNum',
                data:{"id":a,"shopnum":shopnum},
                dataType:"JSON",
                success:function(checknum){
                    if(checknum=="1"){
                        $("#btn").attr("disabled", false);
                    }else{
                        alert("商品库存不足！");
                        $("#btn").attr("disabled", true);
                        $("#"+a).focus();
                    }
                }
            });
        }
        function shop() {
            var checkbox = document.getElementsByName("ids");
            var abc='';
            for (var i = 0; i < checkbox.length; i++) {
                var isok=1;
                if (checkbox[i].checked == true) {
                    var id=checkbox[i].value;
                    var num=document.getElementById(id);
                    if(num.value==null||num.value==""||num.value==0){
                        alert("商品数量选择不正确！");
                        return false;
                    }
                    abc=abc+id+','+num.value+';';

                }
            }
            $("#btn").attr("disabled", true);
            var remarks = $("#remarks").val();
            $.ajax({
                type:'post',
                url:'${ctx}/sys/tbShopController/buyAgentProduct',
                data:{"str":abc,"remarks":remarks},
                dataType:"JSON",
                success:function(a){
                    var url = a.orderUrl;
                    var orderId = a.id;
                    var agentId = a.agentId;
                    window.location.href="saoma?urlApply="+url+"&orderId="+orderId+"&agentId="+agentId+"";
                },
                error:function(b){
                    alert("下单失败");
                }
            });
        }
        function change(a){
            //alert(k);
            var shopnum=0;
            shopnum=$("#"+a).val();
            var agentnum=0;
            $.ajax({
                type:'post',
                url:'${ctx}/sys/tbShopController/checkProductNum',
                data:{"id":a,"shopnum":shopnum},
                dataType:"JSON",
                success:function(checknum){
                    if(checknum=="1"){
                        $("#btn").attr("disabled", false);
                    }else{
                        alert("商品库存不足！");
                        $("#btn").attr("disabled", true);
                        $("#check"+a).removeAttr("checked");
                    }
                }
            });
        }
    </script>

    <style type="text/css">
        .text1{width:100px; height:20px}
    </style>

</head>
<body>
<ul class="nav nav-tabs">
    <li class="active"><a href="#">商品列表</a></li>
</ul>
<form:form id="searchForm" modelAttribute="tbAgentProduct" action="${ctx}/sys/tbShopController/showAgentProductList"
           method="post" class="breadcrumb form-search">
    <div>
        <sys:message content="${message}"/>
        <input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
        <input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
            <label>商品名称：</label><input id="title" name="title" type="text" value="${name}" maxlength="50" class="input-medium" />
         <%--   <span><label>商品类别：</label>
                <select id="box" name="reserveFlag" class="input-medium">
                    <option  value="4" ${tbProduct.reserveFlag=='4'?"selected":""}>便利生活</option>
                    <option  value="3" ${tbProduct.reserveFlag=='3'?"selected":""}>生活预定</option>
                </select>
            </span>--%>
            <span style="margin-top:8px;">
                &nbsp;&nbsp;&nbsp;<input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>&nbsp;&nbsp;
            </span>
            <br/><br/>
    <div/>
            <span><font style="color: #00A6C7">${message1}</font></span>
<table id="contentTable" class="table table-striped table-bordered table-condensed">
    <thead>
        <tr>
            <th>图片</th>
            <th>商品名称</th>
            <th>商品类别</th>
            <th>条形码</th>
            <th>商品规格</th>
            <th>商品货号</th>
            <th>商品价格</th>
            <th>库存</th>
          <%--  <th>是否为预定商品</th>--%>
         <%--   <th>预定起售时间</th>
            <th>预定终售时间</th>--%>
            <th>是否可用麻雀钱包</th>
            <th>选择进货数量</th>
            <th>是否进货</th>
        </tr>
    </thead>
    <tbody>
    <c:forEach items="${page.list}" var="tbAgentProduct">
        <tr>
            <td><img href="${tbAgentProduct.img}" src="${tbAgentProduct.img}" style="width: 50px; height: 40px"/></td>
            <td id="${tbAgentProduct.id}ss">${tbAgentProduct.title}</td>
            <td>
                <c:choose>
                    <c:when test="${tbAgentProduct.reserveFlag=='4'}">
                        便利生活
                    </c:when>
                    <c:when test="${tbAgentProduct.reserveFlag=='3'}">
                        生活预定
                    </c:when>
                </c:choose>
            </td>
            <td>${tbAgentProduct.barcode}</td>
            <td>${tbAgentProduct.attr}</td>
            <td>${tbAgentProduct.unit}</td>
            <td>${tbAgentProduct.price}</td>
            <td>${tbAgentProduct.storeNum}</td>
           <%-- <td>
                <c:choose>
                    <c:when test="${tbAgentProduct.reserveFlag1==1}">
                        <span>是</span>
                    </c:when>
                    <c:otherwise>否</c:otherwise>
                </c:choose>
            </td>--%>
            <%--<c:choose>
                <c:when test="${tbAgentProduct.gender==0}">
                    <td>
                        <fmt:formatDate value="${tbAgentProduct.reserveStartTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
                    </td>
                    <td>
                        <fmt:formatDate value="${tbAgentProduct.reserveEndTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
                    </td>
                </c:when>
                <c:when test="${tbAgentProduct.gender==1}">
                    <td>
                        <fmt:formatDate value="${tbAgentProduct.reserveStartTime}" pattern="HH:mm"/>
                    </td>
                    <td>
                        <fmt:formatDate value="${tbAgentProduct.reserveEndTime}" pattern="HH:mm"/>
                    </td>
                </c:when>
                <c:otherwise>
                    <td>
                    </td>
                    <td>
                    </td>
                </c:otherwise>
            </c:choose>--%>
            <td><c:if test="${tbAgentProduct.isUseMqWallet==0}">否</c:if><c:if test="${tbAgentProduct.isUseMqWallet==1}">是</c:if></td>

            <td><input type="number" class="text1" min="1" id="${tbAgentProduct.id}"  />
            <td><input type="checkbox" name="ids" onchange="change(${tbAgentProduct.id})" value="${tbAgentProduct.id}" id="check${tbAgentProduct.id}" ></td>
        </tr>
    </c:forEach>

    </tbody>
</table>
<div class="control-group">
    <label class="control-label">备注:</label>
    <div class="controls">
        <form:textarea id="remarks" path="remarks" htmlEscape="false" rows="3" maxlength="200" class="input-xlarge"/>
    </div>
</div>
<div style="text-align:left"><input class="btn" id ="btn"type="button" onclick="shop()" value="提交订单" />
</div>
    <div class="pagination">${page}</div>
</form:form>
</body>
</html>
